package com.gmom.tako;

import java.util.List;

import com.gmom.tako.model.Rss;
import com.gmom.tako.model.Url;
import com.gmom.tako.service.TakoService;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RssListActivity extends ListActivity implements AsyncTaskProgress{

	private static List<Rss> rss;
	private static RssAdapter adapter;
	private static Url url= new Url();
	private View progressBar;
	public static String INTENT_RSS = "rss";

	
	
	class RssAdapter extends ArrayAdapter<Rss> {
		
		public RssAdapter(Context context, int textViewResourceId) {
			super(context, textViewResourceId);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			
			Rss item = getItem(position);
			if( row == null) {
				//Viewを作成
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.list_row, null);
			}
			
			if(item != null) {
				//title
				TextView textTitle = (TextView)row.findViewById(R.id.text_title);
				if(textTitle != null) {
					textTitle.setText(item.getTitle().toString());
				}
				
				//detail
				TextView textDetail = (TextView)row.findViewById(R.id.text_detail);
				if(textDetail != null) {
					textDetail.setText(item.getDetail().toString());
				}
				
				//date
				TextView textDate = (TextView)row.findViewById(R.id.text_date);
				if (textDate != null) {
					textDate.setText(item.getDate().toString());
				}
				
			}
			int l = rss.size();
			((TextView)row.findViewById(R.id.text_title)).setTextColor(Color.argb(200,(int)(255*Math.random()), (int)(255*Math.random()), (int)(255*Math.random())));
			row.setBackgroundColor(Color.rgb(255*(position+1)/(l*2), (255*(position+1)/(l*3/2)), 255*(position+1)/l));
			return row;
		}
		
	}

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		//インテントからIDを取得
		//Url url = new Url();
		Intent intent = getIntent();
		String rss_url = intent.getStringExtra(MainActivity.INTENT_URL_URL);
		url.setUrl(rss_url);
		Integer rss_id = intent.getIntExtra(MainActivity.INTENT_URL_ID,1);
		url.set_id(rss_id);
		String rss_title = intent.getStringExtra(MainActivity.INTENT_URL_TITLE);
		url.setTitle(rss_title);
		
        progressBar = findViewById(R.id.progressBar1);
        //DBからデータ取得して、リストで表示
        Intent i=new Intent(this,TakoService.class);
    	startService(i);
        refresh(url);

	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_rss_list, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
        //DBからデータ取得して、リストで表示
    	switch (item.getItemId()) {
		case R.id.menu_settings:
			//インテントからIDを取得
			/*
			Url url = new Url();
			Intent intent = getIntent();
			String rss_url = intent.getStringExtra(MainActivity.INTENT_URL_URL);
			url.setUrl(rss_url);
			Integer rss_id = intent.getIntExtra(MainActivity.INTENT_URL_ID,1);
			url.set_id(rss_id);
			String rss_title = intent.getStringExtra(MainActivity.INTENT_URL_TITLE);
			url.setTitle(rss_title);
*/
	    	refresh(url);
			break;
		default:
			break;
		}
    	return true;
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	Intent intent = new Intent(this, Detail.class);
    	
    	//Log.v("mainactivity", "rss:"+rss);
    	//rssを送る
    	intent.putExtra(INTENT_RSS, rss.get(position));
    	startActivity(intent);
    }

	@Override
	public void onComplete(List<Rss> r) {
		((LinearLayout)findViewById(R.id.LinearLayout1)).removeView(progressBar);
		rss = r;
        for (Rss rss2 : rss) {
			adapter.add(rss2);
		}
        
        
        Toast.makeText(this, getString(R.string.update_complete), Toast.LENGTH_SHORT).show();
		
	}

	
	//起動時と更新ボタン押したとき呼ばれる
	public void refresh(Url url) {
		
		//DBからのデータを入れる
        adapter = new RssAdapter(this, 0);
        setListAdapter(adapter);
                
//        Url url = new Url();
//        url.set_id(1);
                
        new RssReaderTask(this, this).execute(url);
	}

}
